package com.neusoft.neuiotms.dcom.repository.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.common.utils.DateUtil;
import com.neusoft.neuiotms.dcom.model.QTransferWeekly;
import com.neusoft.neuiotms.dcom.model.TransferWeekly;
import com.neusoft.neuiotms.dcom.repository.CustomizedTransferWeeklyRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.dsl.StringExpression;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedTransferWeeklyRepositoryImpl extends AbstractRepository
        implements CustomizedTransferWeeklyRepository {

    @Override
    public Page<TransferWeekly> search(final Pageable pageable, final Searchable searchable) {
        final QTransferWeekly qTransferWeekly = QTransferWeekly.transferWeekly;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<TransferWeekly> query = this.query().selectFrom(qTransferWeekly);
        query.where(where);
        return super.search(query, pageable);
    }

    @Override
    public List<TransferWeekly> index(final Searchable searchable) {
        final QTransferWeekly qTransferWeekly = QTransferWeekly.transferWeekly;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<TransferWeekly> query = this.query().selectFrom(qTransferWeekly);
        query.where(where);
        return query.fetch();
    }

    @Override
    public TransferWeekly getLatestOne(final String unitCode) {
        final QTransferWeekly qTransferWeekly = QTransferWeekly.transferWeekly;
        final BooleanBuilder where = new BooleanBuilder();
        final StringExpression ex = qTransferWeekly.unitCode;
        where.and(ex.containsIgnoreCase(unitCode));
        final JPAQuery<TransferWeekly> query =
                this.query().selectFrom(qTransferWeekly).where(where).orderBy(qTransferWeekly.weekTime.desc()).limit(1);
        final List<TransferWeekly> results = query.fetch();
        return results.size() > 0 ? results.get(0) : null;
    }

    @Override
    protected Class<?> getModelClass() {
        return TransferWeekly.class;
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QTransferWeekly qTransferWeekly = QTransferWeekly.transferWeekly;

        if (!StringUtils.isEmpty(searchable.getStrValue("code"))) {
            where.and(super.containsStr(qTransferWeekly.code, searchable, "code"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("unitCode"))) {
            where.and(super.equalsStr(qTransferWeekly.unitCode, searchable, "unitCode"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("unitName"))) {
            where.and(super.containsStr(qTransferWeekly.unitName, searchable, "unitName"));
        }

        if (null != searchable.getStrArray("institutionCodeArray")) {
            where.and(super.inStrings(qTransferWeekly.unitCode, searchable, "institutionCodeArray"));
        }

        if (null != searchable.getLongValue("weekTime")) {
            final long weekTime = searchable.getLongValue("weekTime");
            final Date startTimeOfWeek = DateUtil.getStartTimeOfWeek(new Date(weekTime));
            final Date endTimeOfWeek = DateUtil.getEndTimeOfWeek(new Date(weekTime));
            where.and(qTransferWeekly.weekTime.before(endTimeOfWeek));
            where.and(qTransferWeekly.weekTime.goe(startTimeOfWeek));
        }

        if (null != searchable.getLongValue("afEqReportDate")) {
            where.and(super.afterOrEqualsDate(qTransferWeekly.weekTime, searchable, "afEqReportDate"));
        }

        if (null != searchable.getLongValue("beEqReportDate")) {
            where.and(super.beforeOrEqualsDate(qTransferWeekly.weekTime, searchable, "beEqReportDate"));
        }

        return where;
    }

}
